<?php

declare(strict_types=1);

namespace App\Model;

use App\Helpers\AliyunUtil;
use Hyperf\DbConnection\Model\Model;

/**
 * 用户互动消息记录
 */
class UserInteractMsg extends Model
{
    protected $table = 'user_interact_msg_log';

    const TYPE_MOMENT_LIKE = 1;
    const TYPE_MOMENT_COMMENT = 2;

    const MOMENT_MSG_TYPE = [self::TYPE_MOMENT_LIKE, self::TYPE_MOMENT_COMMENT];

    protected $fillable = [
        'user_id', 'msg_user_id', 'flag', 'msg_type', 'read', 'msg_object_id', 'detail'
    ];

    function userInfo()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    function msgUserInfo()
    {
        return $this->belongsTo(UserProfile::class, 'msg_user_id', 'user_id');
    }

    function momentInfo()
    {
        return $this->belongsTo(Moment::class, 'msg_object_id', 'id');
    }

    static function addMomentMsg(
        $userId = 0,
        $msgUserId = 0,
        $momentId = 0,
        $type = self::TYPE_MOMENT_LIKE,
        $content = ''
    ) {
        $detail = '';
        if ($type == self::TYPE_MOMENT_COMMENT) {
            $detail = json_encode([
                'content' => $content
            ]);
        }
        $ret = self::create([
            'user_id' => $userId,
            'msg_user_id' => $msgUserId,
            'msg_type' => $type,
            'msg_object_id' => $momentId,
            'read' => 0,
            'detail' => $detail
        ]);
        //推送透传
        Message::sendSlient($msgUserId, 'MOMENT_NEW_MSG');
        return $ret;
    }

    /**
     * 获取用户的动态消息
     */
    static function getMomentMsgs($userId = 0)
    {
        $where = [
            'msg_user_id' => $userId,
            'flag' => 1,
        ];
        $data = self::where($where)->whereIn('msg_type', self::MOMENT_MSG_TYPE)->orderBy('id', 'desc')->get();
        //更新已读
        self::where($where)->whereIn('msg_type', self::MOMENT_MSG_TYPE)->update(['read' => 1]);
        return $data;
    }

    /**
     * 新消息数量
     */
    static function getNewMsgNum($userId = 0)
    {
        $where = [
            'msg_user_id' => $userId,
            'read' => 0,
            'flag' => 1,
        ];
        return self::where($where)->whereIn('msg_type', self::MOMENT_MSG_TYPE)->count();
    }

    /**
     * 构造单个元素
     */
    static function buildItem($item = null)
    {
        switch ($item->msg_type) {
            case self::TYPE_MOMENT_COMMENT:
                $detail = json_decode($item->detail, true);
                $content = $detail['content'] ?? '';
                $msgType = 'comment';
                break;
            case self::TYPE_MOMENT_LIKE:
                $detail = json_decode($item->detail, true);
                $content = '点赞了你的动态';
                $msgType = 'like';
                break;
        }
        $moment = $item->momentInfo;
        if ($moment->video) {
            $type = 'video';
            $momentContent = AliyunUtil::VOICE_HOST . json_decode($moment->video, true)['link'];
        } elseif ($moment->voice) {
            $type = 'video';
            $momentContent = json_decode($moment->voice, true)['duration'] . "S";
        } elseif ($moment->images) {
            $type = 'images';
            $momentContent = AliyunUtil::imageResize(AliyunUtil::IMG_HOST . $moment->images[0], 100, 100);
        } else {
            $type = 'text';
            $momentContent = $moment->words;
        }
        return [
            'moment_id' => $item->msg_object_id,
            'msg_type' => $msgType,
            'user_id' => $item->user_id,
            'nick_name' => $item->userInfo->nick_name,
            'avatar' => UserProfile::buildAvatar($item->userInfo->avatar),
            'content' => $content,
            'moment_info' => [
                'type' => $type,
                'content' => $momentContent,
            ],
            'msg_time' => (string)$item->created_at
        ];
    }
}
